package com.zood.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zood.controller.utils.R;
import com.zood.domain.activity;
import com.zood.service.activityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/activity")
public class activityController {
    @Autowired
    private activityService activityService;

    @GetMapping
    public R getAll(){
        return new R(true, activityService.list());
    }

    @PostMapping
    public R save(@RequestBody activity activity) throws IOException {
        boolean flag = activityService.save(activity);
        return new R(flag, flag ? "添加成功^_^" : "添加失败-_-!");
    }

    @PutMapping
    public R update(@RequestBody activity activity) throws IOException {
        boolean flag = activityService.modify(activity);
        return new R(flag, flag ? "修改成功^_^" : "修改失败-_-!");
    }

    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(activityService.delete(id));
    }

    @GetMapping("{id}")
    public R getById(@PathVariable Integer id){
        return new R(true, activityService.getById(id));
    }


    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage, @PathVariable int pageSize, activity activity){
        IPage<activity> page = activityService.getPage(currentPage, pageSize, activity);
        //如果当前页码值大于总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = activityService.getPage((int)page.getPages(), pageSize, activity);
        }
        return new R(true, page);
    }
}
